package com.example.administrator.uiautomator;

import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.Until;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Created by Administrator on 2017/7/4.
 */


@RunWith(AndroidJUnit4.class)
///说明用androidJunit4
public class HelloWorld {
    //声明一个 UiDevice，因为所有的UIAutomat都通过这个device来实现；
    private UiDevice device;
    // 要测试应用的包名
    private static String PACKAGE_NAME = "com.android.calculator2";

    @Before
    public void setUp() throws Exception {
        //实例化 UiDevice
        device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        //启动应用
        //获取上下文
        Context context = InstrumentationRegistry.getContext();
        //通过将 包名 传给包管理器 获取 启动的 intent
        final Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        //启动应用
        context.startActivity(intent);
        //等待应用 启动
        device.wait(Until.hasObject(By.pkg(PACKAGE_NAME).depth(0)), 1000);

    }

    @Test
    public void name() throws Exception {


        //所有操作对象 都是 UiObject2
        //按钮 1
        BySelector selector1 = By.res("com.android.calculator2:id/digit1");
        UiObject2 one = device.findObject(selector1);
        one.click();

        //按钮 +
        BySelector selectorAdd = By.res("com.android.calculator2:id/plus");
        UiObject2 add = device.findObject(selectorAdd);
        add.click();

        //按钮 2
        BySelector selector2 = By.res("com.android.calculator2:id/digit2");
        UiObject2 two = device.findObject(selector2);
        two.click();

        //按钮 =
        BySelector selectorEqual = By.res("com.android.calculator2:id/equal");
        UiObject2 equal = device.findObject(selectorEqual);
        equal.click();

    }

    @After
    public void tearDown() throws Exception {


    }
}
